/**************************************************************************************
 * Copyright (c) 2014, Marcus Machado, All rights reserved.
 * Copyright (c) 2014, Java Emergency Management Framework - JEMF, All rights reserved.
 * Graduate Program on Informatics (PPGI), Federal University of Rio de Janeiro (UFRJ).
 *  
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *   See the GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 **************************************************************************************/
package br.ufrj.ppgi.jemf.test;

import android.content.ContentProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.test.IsolatedContext;
import android.test.mock.MockContentResolver;
import android.test.mock.MockContext;

/**
 * @author Marcus Machado
 *
 */
public class ResolverMockContext extends IsolatedContext {

    /**
     * The renaming prefix.
     */
    private static final String PREFIX = "br.ufrj.ppgi.jemf.test";

    /**
     * The resolver.
     */
    private static final MockContentResolver RESOLVER = new MockContentResolver();

    /**
     * Constructor.
     * @param context 
     */
    public ResolverMockContext(Context context) {
        super(RESOLVER, new DelegatedMockContext(context));
    }

    /**
     * Get the Resolver instance.
     * @return MockContentResolver
     */
    public MockContentResolver getResolver() {
        return RESOLVER;
    }

    /**
     * Add a new ContentProvider in Resolver.
     * @param name
     * @param provider
     */
    public void addProvider(String name, ContentProvider provider) {
        RESOLVER.addProvider(name, provider);
    }





    /**
     * The DelegatedMockContext.
     */
    private static class DelegatedMockContext extends MockContext {

    	/**
    	 * The Delegated Context.
    	 */
        private Context mDelegatedContext;

        /**
         * Constructor.
         * @param context
         */
        public DelegatedMockContext(Context context) {
            mDelegatedContext = context;
        }

        /**
         * Get the Shared Preferences with a custom prefix.
         * @param name
         * @param mode
         * @return SharedPreferences
         */
        @Override
        public SharedPreferences getSharedPreferences(String name, int mode) {
            return mDelegatedContext.getSharedPreferences(PREFIX + name, mode);
        }

        /**
         * Delete database.
         * @param name
         * @return boolean
         */
		@Override
		public boolean deleteDatabase(String name) {
			return mDelegatedContext.deleteDatabase(name);
		}

        /**
         * Start activity.
         * @param intent
         */
		@Override
		public void startActivity(Intent intent) {
			mDelegatedContext.startActivity(intent);
		}		
		
    }

}
